package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.vo.OrderInfoPayVo;
import com.ktgj.customer.domain.vo.TravelGroupOrderRequest;
import com.ktgj.customer.domain.vo.TravelOrderInfoVo;
import com.ktgj.customer.service.IOrderInfoService;
import com.ktgj.customer.service.impl.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 旅游订单Controller
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@ControllerAdvice
@RestController
@RequestMapping("/info")
public class TravelOrderInfoController extends BaseController
{
    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private LoginServiceImpl loginService;

    /**
     * 查询旅游订单列表
     */
    //@PreAuthorize(hasPermi = "customer:info:list")
    @PostMapping("/travelList")
    public TableDataInfo travelList(@RequestBody OrderInfoDto orderInfoDto)
    {
        startPage();
        List<OrderInfoDto> list = orderInfoService.selectOrderInfoList(orderInfoDto);
        return getDataTable(list);
    }

    /**
     * 导出旅游订单列表
     */
    //@PreAuthorize(hasPermi = "customer:info:export")
    @Log(title = "旅游订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfoDto orderInfoDto) throws IOException
    {
        List<OrderInfoDto> list = orderInfoService.selectOrderInfoList(orderInfoDto);
        ExcelUtil<OrderInfoDto> util = new ExcelUtil<OrderInfoDto>(OrderInfoDto.class);
        util.exportExcel(response, list, "旅游订单数据");
    }

    /**
     * 获取旅游订单详细信息
     */
    //@PreAuthorize(hasPermi = "customer:info:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(orderInfoService.selectOrderInfoById(orderId));
    }

    /**
     * 新增旅游订单
     */
    //@PreAuthorize(hasPermi = "customer:info:add")
    @Log(title = "旅游订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfoDto orderInfoDto)
    {
        return toAjax(orderInfoService.insertOrderInfo(orderInfoDto));
    }

    /**
     * 修改旅游订单
     */
    //@PreAuthorize(hasPermi = "customer:info:edit")
    @Log(title = "旅游订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfoDto orderInfoDto)
    {
        return toAjax(orderInfoService.updateOrderInfo(orderInfoDto));
    }

    /**
     * 删除旅游订单
     */
    //@PreAuthorize(hasPermi = "customer:info:remove")
    @Log(title = "旅游订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(orderInfoService.deleteOrderInfoByIds(orderIds));
    }

    /**
     * 填写旅游订单
     * @param travelGroupOrderRequest
     * @return
     */
    @PostMapping("/createOrderInfo")
    public AjaxResult createOrderInfo(HttpServletRequest request, @RequestBody TravelGroupOrderRequest travelGroupOrderRequest) {
        String token = request.getHeader("token");
        TravelGroupOrderResponse response = orderInfoService.createOrderInfo(token, travelGroupOrderRequest);
        if (response.getCheckPassengersList() != null) {
            return AjaxResult.success(response);
        } else {
            return AjaxResult.error("订单填写错误");
        }
    }

    /**
     * 获取待支付订单
     * @param request
     * @return
     */
    @SignatureValidation
    @PostMapping("/queryByPayOrder")
    public AjaxResult queryByPayOrder(HttpServletRequest request) {
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(orderInfoService.queryByPayOrder(customer.getCustomerId()));
    }

    /**
     * 旅游创建订单
     * */
    @PostMapping("/travelOrder")
    public AjaxResult travelOrder(HttpServletRequest request, @RequestBody TravelOrderInfoVo travelOrderInfoVo) {
        String orderNumber;
        try {
            orderNumber = orderInfoService.travelOrder(request, travelOrderInfoVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (!orderNumber.equals("")){
            return AjaxResult.success(orderNumber);
        }
        else {
            return AjaxResult.error("创建订单失败");
        }
    }

    /**
     * 旅游支付
     * */
    @PostMapping("/orderInfoPay")
    public AjaxResult orderInfoPay(HttpServletRequest request, @RequestBody OrderInfoPayVo orderInfoPayVo){
        String orderInfo;
        try {
            orderInfo = orderInfoService.orderInfoPay(request, orderInfoPayVo);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(orderInfo);
    }

    @PostMapping("/orderRefund")
    public AjaxResult orderRefund(@RequestBody Map<String, Long> map)
    {
        Long orderId = map.get("orderId");
        int rows = 0;
        if (orderId != null)
        {
            rows = orderInfoService.travelOrderRefund(orderId);
        }
        if (rows > 0)
        {
            return AjaxResult.success("旅游订单退单成功！");
        }
        return AjaxResult.error("旅游订单退单失败！");
    }
}
